NGINX 部署
NGINX 是一个高性能的 HTTP 和反向代理服务器,以下是部署 NGINX 的基本步骤:
1. 安装 NGINX
在 Ubuntu/Debian 上安装
sudo apt update
sudo apt install nginx
在 CentOS/RHEL 上安装
sudo yum install epel-release
sudo yum install nginx
2. 启动 NGINX 服务
sudo systemctl start nginx
sudo systemctl enable nginx # 设置开机自启
3. 验证安装
访问服务器 IP 地址,应该能看到 NGINX 欢迎页面。
4. 基本配置
NGINX 的主要配置文件位于:
/etc/nginx/nginx.conf
- 主配置文件/etc/nginx/sites-available/
- 虚拟主机配置/etc/nginx/sites-enabled/
- 启用的虚拟主机(通常是 sites-available 的符号链接)
示例配置(静态网站)
nginx
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
5. 测试并重载配置
sudo nginx -t # 测试配置语法
sudo systemctl reload nginx # 重载配置
6. 防火墙设置
如果使用防火墙,需要允许 HTTP/HTTPS 流量:
sudo ufw allow 'Nginx Full' # Ubuntu
# 或
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload # CentOS
7. SSL 证书配置(可选)
使用 Let's Encrypt 免费 SSL 证书:
sudo apt install certbot python3-certbot-nginx # Ubuntu
sudo certbot --nginx -d example.com -d www.example.com
8. 常用命令
- 启动:
sudo systemctl start nginx
- 停止:
sudo systemctl stop nginx
- 重启:
sudo systemctl restart nginx
- 重载配置:
sudo systemctl reload nginx
- 检查状态:
sudo systemctl status nginx
- 测试配置:
sudo nginx -t
高级功能
反向代理:
nginx
location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }
负载均衡:
nginx
upstream backend { server backend1.example.com; server backend2.example.com; } server { location / { proxy_pass http://backend; } }
缓存配置:
nginx
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m inactive=60m; server { location / { proxy_cache my_cache; proxy_pass http://backend; } }
Nginx反向代理jumpserver配置参考
Nginx服务器ip:10.200.11.17
jumpserver服务器ip:10.200.11.15
域名:以实际域名填写,我这边以jumpserver.xxxx.com为例
# /etc/nginx/conf.d/jumpserver.conf
# 主HTTP服务(监听8080,用于HTTP跳转HTTPS)
server {
listen 8080;
server_name jumpserver.xxxx.com;
return 301 https://$server_name$request_uri; # 跳转到HTTPS
}
# HTTPS核心服务
server {
listen 443 ssl http2;
server_name jumpserver.xxxx.com;
# SSL证书配置
ssl_certificate /etc/nginx/ssl/jumpserver.xxxx.com.pem;
ssl_certificate_key /etc/nginx/ssl/jumpserver.xxxx.com.key;
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_protocols TLSv1.1 TLSv1.2;
add_header Strict-Transport-Security "max-age=63072000" always;
client_max_body_size 5000m; # 上传文件大小限制
# 日志配置
access_log /var/log/nginx/jumpserver.access.log;
error_log /var/log/nginx/jumpserver.error.log;
# 代理配置
location / {
proxy_pass http://10.200.11.15:80; # 直接指向Docker的80端口
proxy_http_version 1.1;
proxy_buffering off;
proxy_request_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /jms {
# 将 proxy_pass 替换成对应服务地址即可
proxy_pass http://10.200.11.15:80;
rewrite "/jms/(.*)$" /$1 break;
proxy_http_version 1.1;
proxy_buffering off;
proxy_request_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect ~^/(.*) https://$http_host/jms/$1;
sub_filter_types *;
sub_filter_once off;
sub_filter '/static/' '/jms\/static/';
sub_filter '/ui/' '/jms/';
sub_filter '/api/' '/jms/api/';
sub_filter '/core/' '/jms/core/';
sub_filter '/ws/notifications/' '/jms/ws/notifications/';
sub_filter '/luna/' '/jms/luna/';
sub_filter '/koko/' '/jms/koko/';
sub_filter '/lion' '/jms/lion';
sub_filter '/media/' '/jms/media/';
sub_filter '/omnidb/' '/jms/omnidb/';
}
}
#/etc/nginx/nginx.conf 新增如下配置
#代理2222端口
stream {
upstream ssh_backend {
server 10.200.11.15:2222;#jumpserver ip地址
}
server {
listen 2222;
proxy_pass ssh_backend;
}
}
注意
v3.6 版本为了安全,要求强制填写 DOMAINS 可信任域名才能正常访问服务,否则会提示错误码 400/403 导致无法无法访问页面,DOMAINS 配置如下。
如果服务器是一键安装并且旧版本就已经使用 JumpServer 开启了 HTTPS,则不需要进行任何更改。 需要使用 IP 地址来访问 JumpServer 的场景,可以根据自己的 IP 类型来填写 config.txt 配置文件中 DOMAINS 字段为公网 IP 还是内网 IP。
打开config.txt 配置文件,定义 DOMAINS 字段 vim /opt/jumpserver/config/config.txt
可信任 DOMAINS 定义,
定义可信任的访问 IP, 请根据实际情况修改, 如果是公网 IP 请改成对应的公网 IP。
例如:DOMAINS="demo.jumpserver.org" # 使用域名访问
例如:DOMAINS="172.17.200.191" # 使用 IP 访问
例如:DOMAINS="demo.jumpserver.org,172.17.200.191" # 使用 IP 和 域名一起访问
DOMAINS="jumpserver.xxxx.com,10.200.11.15" #实际环境中我的配置
重启 JumpServer 服务生效
jmsctl restart